package main.java.com.healthy.entity;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 营养目标实体类
 * 对应功能模块：营养目标与计划模块
 */
public class NutritionGoal implements Serializable {
    private static final long serialVersionUID = 1L;

    private String goalId;
    private String userId;
    private double dailyCalorieGoal; // 每日热量目标(千卡)
    private double dailyCarbsGoal; // 每日碳水目标(g)
    private double dailyProteinGoal; // 每日蛋白质目标(g)
    private double dailyFatGoal; // 每日脂肪目标(g)
    private double dailyFiberGoal; // 每日膳食纤维目标(g)
    private double dailySugarLimit; // 每日糖分限制(g)
    private double dailySodiumLimit; // 每日钠限制(mg)
    private String targetType; // 目标类型：减重、维持、增肌
    private double weeklyWeightChange; // 每周体重变化目标(kg)
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private boolean isActive; // 是否激活

    // 默认构造方法
    public NutritionGoal() {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.isActive = true;
    }

    // 主要参数构造方法
    public NutritionGoal(String goalId, String userId, double dailyCalorieGoal,
                         double dailyCarbsGoal, double dailyProteinGoal, double dailyFatGoal,
                         String targetType) {
        this();
        this.goalId = goalId;
        this.userId = userId;
        this.dailyCalorieGoal = dailyCalorieGoal;
        this.dailyCarbsGoal = dailyCarbsGoal;
        this.dailyProteinGoal = dailyProteinGoal;
        this.dailyFatGoal = dailyFatGoal;
        this.targetType = targetType;
    }

    // Getter和Setter方法
    public String getGoalId() {
        return goalId;
    }

    public void setGoalId(String goalId) {
        this.goalId = goalId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public double getDailyCalorieGoal() {
        return dailyCalorieGoal;
    }

    public void setDailyCalorieGoal(double dailyCalorieGoal) {
        this.dailyCalorieGoal = dailyCalorieGoal;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailyCarbsGoal() {
        return dailyCarbsGoal;
    }

    public void setDailyCarbsGoal(double dailyCarbsGoal) {
        this.dailyCarbsGoal = dailyCarbsGoal;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailyProteinGoal() {
        return dailyProteinGoal;
    }

    public void setDailyProteinGoal(double dailyProteinGoal) {
        this.dailyProteinGoal = dailyProteinGoal;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailyFatGoal() {
        return dailyFatGoal;
    }

    public void setDailyFatGoal(double dailyFatGoal) {
        this.dailyFatGoal = dailyFatGoal;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailyFiberGoal() {
        return dailyFiberGoal;
    }

    public void setDailyFiberGoal(double dailyFiberGoal) {
        this.dailyFiberGoal = dailyFiberGoal;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailySugarLimit() {
        return dailySugarLimit;
    }

    public void setDailySugarLimit(double dailySugarLimit) {
        this.dailySugarLimit = dailySugarLimit;
        this.updateTime = LocalDateTime.now();
    }

    public double getDailySodiumLimit() {
        return dailySodiumLimit;
    }

    public void setDailySodiumLimit(double dailySodiumLimit) {
        this.dailySodiumLimit = dailySodiumLimit;
        this.updateTime = LocalDateTime.now();
    }

    public String getTargetType() {
        return targetType;
    }

    public void setTargetType(String targetType) {
        this.targetType = targetType;
        this.updateTime = LocalDateTime.now();
    }

    public double getWeeklyWeightChange() {
        return weeklyWeightChange;
    }

    public void setWeeklyWeightChange(double weeklyWeightChange) {
        this.weeklyWeightChange = weeklyWeightChange;
        this.updateTime = LocalDateTime.now();
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
        this.updateTime = LocalDateTime.now();
    }

    // 计算宏量营养素比例
    public double getCarbsCaloriePercentage() {
        double carbsCalories = dailyCarbsGoal * 4;
        return dailyCalorieGoal > 0 ? (carbsCalories / dailyCalorieGoal) * 100 : 0;
    }

    public double getProteinCaloriePercentage() {
        double proteinCalories = dailyProteinGoal * 4;
        return dailyCalorieGoal > 0 ? (proteinCalories / dailyCalorieGoal) * 100 : 0;
    }

    public double getFatCaloriePercentage() {
        double fatCalories = dailyFatGoal * 9;
        return dailyCalorieGoal > 0 ? (fatCalories / dailyCalorieGoal) * 100 : 0;
    }

    // 验证目标是否合理
    public boolean isValid() {
        double totalPercentage = getCarbsCaloriePercentage() + getProteinCaloriePercentage() + getFatCaloriePercentage();
        return totalPercentage >= 95 && totalPercentage <= 105; // 允许5%的误差
    }

    // 获取建议的宏量营养素分配
    public String getRecommendedDistribution() {
        switch (targetType) {
            case "减重":
                return "碳水化合物: 40-50%, 蛋白质: 25-35%, 脂肪: 20-30%";
            case "增肌":
                return "碳水化合物: 45-55%, 蛋白质: 25-35%, 脂肪: 15-25%";
            case "维持":
            default:
                return "碳水化合物: 45-65%, 蛋白质: 10-35%, 脂肪: 20-35%";
        }
    }

    @Override
    public String toString() {
        return "NutritionGoal{" +
                "goalId='" + goalId + '\'' +
                ", userId='" + userId + '\'' +
                ", dailyCalorieGoal=" + dailyCalorieGoal +
                ", dailyCarbsGoal=" + dailyCarbsGoal +
                ", dailyProteinGoal=" + dailyProteinGoal +
                ", dailyFatGoal=" + dailyFatGoal +
                ", targetType='" + targetType + '\'' +
                ", weeklyWeightChange=" + weeklyWeightChange +
                ", isActive=" + isActive +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NutritionGoal that = (NutritionGoal) o;
        return goalId.equals(that.goalId);
    }

    @Override
    public int hashCode() {
        return goalId.hashCode();
    }
}
